package com.zuqiukong.automaticpacking.listener;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.zuqiukong.automaticpacking.Constants;
import com.zuqiukong.automaticpacking.ProcessUtils;
import com.zuqiukong.automaticpacking.ProcessUtils.LineMsgHandle;
import com.zuqiukong.automaticpacking.taskheadler.QueuesHeadler;

/**
 * @author jie
 */
public class MyServletContextListener implements ServletContextListener {

    private Timer timer = null;

    public void contextDestroyed(ServletContextEvent event) {
        QueuesHeadler.cannel();
        timer.cancel();
        event.getServletContext().log("定时器销毁");
        Constants.mServletContext = null;
    }


    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        Constants.mServletContext = arg0.getServletContext();
        Constants.WebPath = arg0.getServletContext().getRealPath("/") ;
        initProperties(arg0);


        new Thread(new QueuesHeadler()).start();

//		timer = new Timer(true);
//	    timer.schedule(new MyTask(),1000,60000);
    }

    /**
     * 读取配置文件，初始化打包环境变量
     */
    private void initProperties(ServletContextEvent arg1) {
        Properties pps = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(arg1.getServletContext().getRealPath("/") + "WEB-INF/classes/setting.properties"));
            pps.load(in);
            Constants.PROJECT_PATH = arg1.getServletContext().getRealPath("/");
            Constants.PROJECT_GIT_REMOTE = pps.getProperty("PROJECT_GIT_REMOTE");
            Constants.PROJECT_GIT_BRANCH = pps.getProperty("PROJECT_GIT_BRANCH");
            Constants.PROJECT_GIT_BRANCH_BETA = pps.getProperty("PROJECT_GIT_BRANCH_BETA");
            Constants.PROJECT_PATH_BASE = pps.getProperty("PROJECT_PATH_BASE");
            Constants.PROJECT_CHECK_VERSION_PATH = Constants.PROJECT_PATH_BASE + pps.getProperty("PROJECT_CHECK_VERSION_PATH");
            Constants.PROJECT_CHECK_VERSION_BUILD_GRADLE_PATH = Constants.PROJECT_CHECK_VERSION_PATH + pps.getProperty("BUILD_GRADLE_PATH");
            Constants.APK_PATH = pps.getProperty("APK_PATH");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//	public static class MyTask extends TimerTask{
//        private int hasNew;
//
//
//		@Override
//        public void run()
//        {
//        	Constants.log("执行beta版更新:"+Constants.IsPackingBeta);
//        	if(1 != Constants.IsPackingBeta )
//			{
//				int code = checkNewcode();
//				if(1 == code)
//				{
//					Constants.IsPackingBeta = 1;
//					new Thread(new Runnable()
//					{
//						@Override
//						public void run()
//						{
//							new ChannelTaskBeta().doWrok();
//						}
//					}).start();
//				}
//				else if(2 == code)
//				{
//					Constants.IsPackingBeta = 0;
//				}
//				else
//				{
//					Constants.IsPackingBeta = -1;
//				}
//			}
//        }
//
//


}
